package com.cykj.service.Impl;

import com.cykj.dto.ResponseDto;
import com.cykj.mapper.UserMapper;
import com.cykj.pojo.PsAdmin;
import com.cykj.pojo.PsUser;
import com.cykj.service.UserService;
import com.cykj.util.ImageCodeUtils;
import com.cykj.util.JwtUtils;
import com.cykj.util.MD5Util;
import com.cykj.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @BelongsProject: Psychology
 * @BelongsPackage: com.cykj.service.Impl
 * @Author: 小林
 * @CreateTime: 2025-02-10 09:04
 * @Description: 用户service层
 * @Version: 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 登录
     *
     * @param acc
     * @param pwd
     * @param code
     * @return
     */
    @Override
    public ResponseDto login(String acc, String pwd, String code) {
        ImageCodeUtils imageCodeUtils = new ImageCodeUtils();
        String code1 = imageCodeUtils.getCode();
        String encrypt = MD5Util.encrypt(pwd);
        PsUser user = userMapper.login(acc, encrypt, code);
        if (user != null) {
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("userId", user.getUserId());
            hashMap.put("userName", user.getUserName());

            //生成token
            String token = JwtUtils.generateJwt(hashMap);
            return ResponseDto.success(token);
        }
        return ResponseDto.error("账号或密码错误！");
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public ResponseDto reg(PsUser user) {
        if (userMapper.selectAccount(user) == null || userMapper.selectAccount(user).size() == 0) {
            return userMapper.reg(user) == 1 ?
                    ResponseDto.success("注册成功") :
                    ResponseDto.error("注册失败");
        }
        return ResponseDto.error("账号已存在！！！");
    }

    /**
     * 充值
     * @param vo
     * @return
     */
    @Override
    public ResponseDto recharge(UserVo vo) {
        int i = userMapper.recharge(vo);
        if (i > 0) {
           return ResponseDto.success("充值成功！");
        }
        return ResponseDto.error("充值失败");
    }

    /**
     * 查询用户余额
     * @param vo
     * @return
     */
    @Override
    public ResponseDto selectUserBalance(UserVo vo) {
       PsUser user =  userMapper.selectUserBalance(vo);
       return user!=null? ResponseDto.success(user):ResponseDto.error("查询失败");
    }

    /**
     * 用户扣钱
     * @param vo
     * @return
     */
    @Override
    public ResponseDto deductions(UserVo vo) {
        int i = userMapper.deductions(vo);
        if (i > 0) {
            return ResponseDto.success("扣除成功！");
        }
        return ResponseDto.error("扣除失败");
    }

    /**
     * 查询咨询师余额
     * @param vo
     * @return
     */
    @Override
    public ResponseDto addMoney(UserVo vo) {
        int i = userMapper.addMoney(vo);
        if (i > 0) {
            return ResponseDto.success("添加成功！");
        }
        return ResponseDto.error("添加失败");
    }
}

